package com.nxn.core.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.nxn.core.dao.GenericDao;
import com.nxn.core.service.GenericManager;
import com.nxn.core.utils.Page;

/**
 * This class serves as the Base class for all other Managers - namely to hold
 * common CRUD methods that they might all use. You should only need to extend
 * this class when your require custom CRUD logic.
 * <p/>
 * <p>To register this class in your Spring context file, use the following XML.
 * <pre>
 *     &lt;bean id="userManager" class="com.nxn.core.service.impl.GenericManagerImpl"&gt;
 *         &lt;constructor-arg&gt;
 *             &lt;bean class="com.nxn.core.dao.hibernate.GenericDaoHibernate"&gt;
 *                 &lt;constructor-arg value="com.nxn.core.model.User"/&gt;
 *                 &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
 *             &lt;/bean&gt;
 *         &lt;/constructor-arg&gt;
 *     &lt;/bean&gt;
 * </pre>
 * <p/>
 * <p>If you're using iBATIS instead of Hibernate, use:
 * <pre>
 *     &lt;bean id="userManager" class="com.nxn.core.service.impl.GenericManagerImpl"&gt;
 *         &lt;constructor-arg&gt;
 *             &lt;bean class="com.nxn.core.dao.ibatis.GenericDaoiBatis"&gt;
 *                 &lt;constructor-arg value="com.nxn.core.model.User"/&gt;
 *                 &lt;property name="dataSource" ref="dataSource"/&gt;
 *                 &lt;property name="sqlMapClient" ref="sqlMapClient"/&gt;
 *             &lt;/bean&gt;
 *         &lt;/constructor-arg&gt;
 *     &lt;/bean&gt;
 * </pre>
 *
 * @param <T>  a type variable
 * @param <PK> the primary key for that type
 * @author Michael
 */
public abstract class GenericManagerImpl<T, PK extends Serializable> implements GenericManager<T, PK> {
    /**
     * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
     */
    protected final Log log = LogFactory.getLog(getClass());

	public abstract GenericDao<T, PK> getSearchImpl();
	
	public T get(Class<T> entityClass, PK id) {
		return getSearchImpl().get(entityClass, id);
	}

    public List<T> getAll(Class<T> entityClass) {
        return getSearchImpl().getAll(entityClass);
    }
    
	public Page<T> queryPage(Page<T> page, String hql, Object... values) {
		return getSearchImpl().queryPage(page, hql, values);
	}
    
    public Page<T> queryPage(Page<T> page, String hql, Map<String, ?> values) {
    	return getSearchImpl().queryPage(page, hql, values);
    }

    public boolean exists(Class<T> entityClass, PK id) {
        return getSearchImpl().exists(entityClass, id);
    }

    public T save(T entity) {
        return getSearchImpl().save(entity);
    }

    public void remove(T entity) {
    	getSearchImpl().remove(entity);
    }
    
}
